/Finding majority element in the array O(n)
#include <bits/stdc++.h> 
using namespace std; 
  
int findMaj(int a[], int size) 
{ 
    int maj = 0, count = 1;  //assuming first element as majority with count 1
    for (int i = 1; i < size; i++) 
    { 
        if (a[maj] == a[i]) 
            count++; 
        else
            count--; 
        if (count == 0)        //if count<0 reconsider majority element
        { 
            maj = i; 
            count = 1; 
        } 
    } 
    return a[maj]; 
} 
  
//checks for no. of occurences i.e majority if no. > n/2
int isMaj(int a[], int size, int cand) 
{ 
    int count = 0; 
    for (int i = 0; i < size; i++) 
      
    if (a[i] == cand) 
    count++; 
          
    if (count > size/2) 
    return 1; 
      
    else
    return 0; 
}  
  
  
int main() 
{ 
    int a[] = {1, 3, 3, 1, 2, 4 , 9, 7}; 
    int size = (sizeof(a))/sizeof(a[0]); 
    int majCand = findMaj(a,size); //finding majority candidate
    
    if(isMaj(a,size,majCand))
      cout << majCand;
    else
      cout << "No Majority candidate";
      
    return 0; 
}
